package com.gis.controller;


import com.gis.utils.CommonUtil;
import com.gis.utils.PostgresqlUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
@RequiredArgsConstructor
@RequestMapping("/postgresql")
public class PostgresqlController {
    private final PostgresqlUtil postgresqlUtil;
    private final CommonUtil commonUtil;

    @PostMapping("/uploadShp")
    public ResponseEntity<String> uploadShpFile(@RequestParam("file") MultipartFile file) {
        try {
            // 将MultipartFile转换为File对象
            File convertedFile = commonUtil.convertMultipartFileToFile(file);

            postgresqlUtil.importShpFileMethod(convertedFile);
            // 删除临时文件
//            convertedFile.delete();

            return ResponseEntity.ok("Shapefile uploaded and published successfully: ");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("Failed to upload Shapefile: " + e.getMessage());
        }
    }
}
